<template>
  <div class="edition">
    <div class="title">
      <p class="tip">版本发布单</p>
      <button @click="back" class="goBack">返回</button>
    </div>
    <div class="contain">
      <el-container>
        <el-header>
          <el-scrollbar>
            <el-menu
              :default-active="activeIndex"
              class="el-menu-demo"
              mode="horizontal"
              @select="handleSelect"
              :ellipsis="false"
            >
              <el-menu-item index="1">已发布</el-menu-item>
              <el-menu-item index="2">已下载</el-menu-item>
              <!--   <el-menu-item index="6">处理超时</el-menu-item> -->
              <el-menu-item index="3">未下载</el-menu-item>
              <el-menu-item index="4">已完成</el-menu-item>
              <el-menu-item index="5">未完成</el-menu-item>
              <el-menu-item index="6">已发布（全年）</el-menu-item>
              <el-menu-item index="7">已下载（全年）</el-menu-item>
              <el-menu-item index="8">未下载（全年）</el-menu-item>
              <el-menu-item index="9">已完成（全年）</el-menu-item>
              <el-menu-item index="10">未完成（全年）</el-menu-item>
            </el-menu>
          </el-scrollbar>
        </el-header>
        <el-main>
          <div class="main">
            <el-form ref="searchForm" :inline="true" class="demo-form-inline">
              <el-form-item label="系统名称">
                <!-- <el-autocomplete
                  clearable
                  class="inline-input"
                  v-model="state"
                  :fetch-suggestions="querySearch"
                  placeholder="请输入系统名称"
                  :trigger-on-focus="false"
                  @select="handleSelect_orgId"
                ></el-autocomplete> -->
                <el-select
                  v-model="state"
                  placeholder="请选择系统"
                  style="width: 240px"
                  filterable
                  clearable
                  @change="handleSelect_orgId"
                >
                  <el-option
                    v-for="(item, index) in restaurants"
                    :key="index + 1"
                    :label="item.value"
                    :value="item.orgId"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="单号">
                <el-input v-model="changeCode" class="inline-input" placeholder="请输入单号" />
              </el-form-item>
              <el-form-item label="标题">
                <el-input
                  clearable
                  v-model="changeTitle"
                  class="inline-input"
                  placeholder="请输入标题"
                />
              </el-form-item>
              <el-form-item label="所属机构">
                <el-input
                  clearable
                  v-model="Organization"
                  class="inline-input"
                  placeholder="请输入机构名称"
                />
              </el-form-item>
            </el-form>
            <div class="button">
              <button type="primary" @click="detail" class="btn xq" v-if="false">
                查看版本详情
              </button>
              <button @click="task" class="btn rw xq" v-if="false"> 查看任务详情 </button>

              <button @click="search" @keyup.enter="monitor()" class="btn reset"> 搜索 </button>
              <button @click="resetForm" class="btn">重置</button>

              <el-dropdown trigger="click">
                <span class="el-dropdown-link">
                  导出<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item icon="el-icon-plus" @click="downloadExcel">
                      导出当前页
                    </el-dropdown-item>
                    <el-dropdown-item icon="el-icon-circle-plus" @click="Select_downloadExcel">
                      导出所有页
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
          <div class="tableBox">
            <div class="desc" v-if="false">
              <img src="../../assets/images/bbsm.png" alt="" />

              <div v-if="!grop">
                <!-- -->
                <ul class="organ">
                  <!-- <li v-for="(item, index) in 8" :key="index">
                                应用{{ item }}
                            </li> -->
                  <li v-for="(item, index) in gropList" :key="index">
                    {{ item?.orgName }}：{{ item?.orgCount }}
                  </li>
                </ul>
              </div>
              <div class="Noorgan" v-if="grop">
                <span>暂无机构状态</span>
              </div>
            </div>
            <div class="table" ref="container">
              <el-table
                :height="tableHeight"
                class="table"
                ref="multipleTableRef"
                :data="changeList"
                tooltip-effect="dark"
                @select="selectChange"
                @selection-change="handleSelectionChange"
                row-key="getRowKey"
                @row-click="handleRowClick"
                v-loading="loading"
                element-loading-text="加载中"
              >
                <el-table-column align="center" :reserve-selection="false" type="selection">
                </el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="versionInfoNo"
                  label="单号"
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="versionInfoName"
                  label="标题"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column align="center" prop="sysName" label="系统名" show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="orgName"
                  label="所属机构"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="taskNo"
                  label="任务单号"
                  show-overflow-tooltip
                >
                  <!-- v-if="Exhibition" -->
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="actualStartTime"
                  label="升级时间"
                  show-overflow-tooltip
                >
                  <!-- v-if="Exhibition" -->
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="actualFinishTime"
                  label="升级结束时间"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="taskStatus"
                  label="任务状态"
                  show-overflow-tooltip
                />
                <!-- v-if="Exhibition" -->
                <el-table-column
                  align="center"
                  prop="systemStage"
                  label="系统所处阶段"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="useApprovalName"
                  label="应用管理员"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="buildDept"
                  label="建设部门"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="buildSubDept"
                  label="建设处室"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="producerName"
                  label="项目经理"
                  show-overflow-tooltip
                />
                <!--  <el-table-column
                  align="center"
                  prop="producerPhone"
                  label="项目经理电话"
                  show-overflow-tooltip
                  v-if="right ? true : false"
                /> -->
                <el-table-column
                  align="center"
                  prop="completeName"
                  label="实施人"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="businessYzName"
                  label="业务验证人"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="nextIds"
                  label="当前处理人"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="nextPhones"
                  label="当前处理人电话"
                  show-overflow-tooltip
                />
                <el-table-column
                  align="center"
                  prop="isAutomate"
                  label="是否自动化"
                  show-overflow-tooltip
                  v-if="activeIndex == '5' ? true : false"
                />
                <el-table-column
                  align="center"
                  prop="ifResultSmoothly"
                  label="升级操作结果"
                  show-overflow-tooltip
                  v-if="activeIndex == '5' ? true : false"
                />
                <el-table-column
                  align="center"
                  prop="healthResult"
                  label="健康检查结果（含是否超4点）"
                  show-overflow-tooltip
                  v-if="activeIndex == '5' ? true : false"
                />
                <el-table-column
                  align="center"
                  prop="ifForCs"
                  label="健康检查是否超4点"
                  show-overflow-tooltip
                  v-if="activeIndex == '5' ? true : false"
                />
                <el-table-column
                  align="center"
                  prop="businessResult"
                  label="业务验证结果"
                  show-overflow-tooltip
                  v-if="activeIndex == '5' ? true : false"
                />
              </el-table>
            </div>

            <!-- 分页 -->
            <el-pagination
              ref="pagination"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              v-model:currentPage="pageNum"
              :page-sizes="[10, 15, 20]"
              v-model:page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            ></el-pagination>
          </div>
        </el-main>
      </el-container>
    </div>
  </div>
</template>
<script setup lang="ts">
  import { ElMessage } from 'element-plus';
  import { ref, onMounted, onBeforeUnmount, reactive, watch } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  import { changeOrganization } from '@/http/api/resources';
  import {
    selectTaskInfoProvinceList,
    selectTaskInfoProvinceExport,
    // selectTaskInfoProvinceListCount,
    download
  } from '@/http/api/edition';
  import { useStore } from 'vuex';
  const store = useStore();
  const router = useRouter();
  const route = useRoute();
  const changeList = reactive([]);
  const pageNum = ref(1);
  const pageSize = ref(10);
  const total = ref(0);
  const changeTitle = ref('');
  const changeCode = ref('');
  const state = ref('');
  const orgId = ref('');
  //const restaurants = reactive([]);
  const restaurants = ref<RestaurantItem[]>([]);
  const tableHeight = ref(0);
  // const right = ref(true);
  // const Exhibition = ref(false);
  const Organization = ref('');
  // const gropList = reactive([]);
  const container = ref(null);
  // const ifBigFault = ref();
  // const grop = ref(false);
  const loading = ref(true);
  const startTime = ref(
    route.params.startTime ? route.params.startTime : store.state.Provincial_startTime
  );
  // const type = ref(''); // route.params.type
  const endTime = ref(route.params.endTime ? route.params.endTime : store.state.Provincial_endTime);
  const activeIndex = ref(route.params.Index ? route.params.Index : '1');
  const multipleTableRef = ref<InstanceType<typeof ElTable>>();
  const multipleTable = ref<User[]>([]);
  // const flag = ref('');
  onMounted(() => {
    getPage();
    enterKeyup();
    calHeight();
    changeOrgan();
    type_count();
  });
  const getPage = () => {
    const localPage = localStorage.getItem('editionListPage');
    if (localPage != null) {
      pageNum.value = Number(localPage);
    } else {
      pageNum.value = 1;
    }
  };
  const type_count = () => {
    getVmchangelist();
    // getGrouplist();
  };
  const handleSelect = (key: any) => {
    activeIndex.value = key;
    loading.value = true;
    type_count();
  };
  const getVmchangelist = () => {
    selectTaskInfoProvinceList({
      startTime: startTime.value,
      endTime: endTime.value,
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      flag: activeIndex.value,
      versionInfoNo: changeCode.value,
      versionInfoName: changeTitle.value,
      sysid: orgId.value,
      extraOrgName: Organization.value
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        loading.value = false;
        return;
      }
      loading.value = false;
      changeList.length = 0;
      (changeList as any).push(...res.data.rows);
      total.value = res.data.total;
      if (total.value == 0) {
        grop.value = true;
      } else {
        grop.value = false;
      }
    });
  };
  // const getGrouplist = () => {
  //   selectTaskInfoProvinceListCount({
  //     startTime: startTime.value,
  //     endTime: endTime.value,
  //     flag: activeIndex.value
  //   }).then((res) => {
  //     if ((res as any).data.code !== 0) {
  //       ElMessage({
  //         message: (res as any).data.msg,
  //         type: 'error'
  //       });
  //       return;
  //     }
  //     gropList.length = 0;
  //     (gropList as any).push(...res.data.data);
  //     if (total.value == 0) {
  //       grop.value = true;
  //     } else {
  //       grop.value = false;
  //     }
  //   });
  // };
  const changeOrgan = () => {
    changeOrganization().then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      restaurants.value.length = 0;
      res.data.data.forEach((item) => {
        restaurants.value.push({
          value: item.orgName,
          orgId: item.orgId
        });
      });
    });
  };
  const monitor = () => {
    document.onkeydown = (e) => {
      if (e.keyCode == 13) {
        search();
      }
    };
  };
  const enterKeyupDestroyed = () => {
    document.removeEventListener('keyup', monitor());
  };
  const enterKeyup = () => {
    document.addEventListener('keyup', monitor());
  };
  const search = () => {
    if (!state.value) {
      orgId.value = '';
    }
    pageNum.value = 1;
    changeTitle.value = changeTitle.value.replace(/(^\s*)|(\s*$)/g, '');
    changeCode.value = changeCode.value.replace(/(^\s*)|(\s*$)/g, '');
    state.value = state.value.replace(/(^\s*)|(\s*$)/g, '');
    Organization.value = Organization.value.replace(/(^\s*)|(\s*$)/g, '');
    loading.value = true;
    type_count();
  };
  const resetForm = () => {
    changeTitle.value = '';
    changeCode.value = '';
    state.value = '';
    orgId.value = '';
    Organization.value = '';
  };
  // 分页
  const handleSizeChange = (size) => {
    pageSize.value = size;
    loading.value = true;
    type_count();
  };
  const handleCurrentChange = (num) => {
    pageNum.value = num;
    loading.value = true;
    localStorage.setItem('editionListPage', num);
    type_count();
  };
  const back = () => {
    router.push({
      name: 'restructure'
    });
  };
  const calHeight = () => {
    const rect = container.value.getBoundingClientRect();
    tableHeight.value = rect.height;
  };
  // 点击行触发，选中或不选中复选框
  const handleRowClick = (row) => {
    multipleTable.value = row;
    multipleTableRef.value.toggleRowSelection(row);
    selectChange(multipleTable.value);
  };
  const selectChange = (selection: string) => {
    if (selection.length > 1) {
      const del_row = selection.shift();
      multipleTableRef.value!.toggleRowSelection(del_row);
    }
  };
  const handleSelectionChange = (val: string) => {
    multipleTable.value = val;
  };
  const handleSelect_orgId = (item: RestaurantItem) => {
    orgId.value = item;
  };
  const detail = () => {
    if (!multipleTable.value.length) {
      ElMessage({
        message: '请先勾选查看的数据',
        type: 'warning'
      });
    } else {
      router.push({
        name: 'edition_detail',
        params: {
          value: multipleTable.value[0].versionInfoId,
          type: activeIndex.value
        }
      });
    }
  };
  const task = () => {
    if (!multipleTable.value.length) {
      ElMessage({
        message: '请先勾选查看的数据',
        type: 'warning'
      });
    } else {
      router.push({
        name: 'editionTask_detail',
        params: {
          taskId: multipleTable.value[0].taskId,
          type: activeIndex.value
        }
      });
    }
  };
  // 导出当前页
  const downloadExcel = () => {
    selectTaskInfoProvinceExport({
      startTime: startTime.value,
      endTime: endTime.value,
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      versionInfoNo: changeCode.value,
      versionInfoName: changeTitle.value,
      sysid: orgId.value,
      extraOrgName: Organization.value,
      flag: activeIndex.value,
      currentPage: 'currentPage'
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      downloadlist(res.data.msg);
    });
  };

  const downloadlist = (type) => {
    download({
      fileName: type,
      delete: true
    }).then((res) => {
      const link = document.createElement('a');
      let blob = new Blob([res.data], {
        type: 'application/vnd.ms-excel'
      });
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);
      link.download = type; //下载的文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    });
  };
  // 导出所有
  const Select_downloadExcel = () => {
    selectTaskInfoProvinceExport({
      startTime: startTime.value,
      endTime: endTime.value,
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      versionInfoNo: changeCode.value,
      versionInfoName: changeTitle.value,
      sysName: state.value,
      extraOrgName: Organization.value,
      flag: activeIndex.value,
      currentPage: 'all'
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      downloadlist(res.data.msg);
    });
  };
  onBeforeUnmount(() => {
    enterKeyupDestroyed();
  });
  watch(
    () => router.options.history.state.back,
    (newVal) => {
      if (newVal == '/restructure') {
        localStorage.removeItem('editionListPage');
      }
    },
    { immediate: true }
  );
</script>

<style scoped lang="less">
  .edition {
    width: 100%;
    height: 100vh;
    background: #eaeef4;
    padding: 20px;
    box-sizing: border-box;
    .title {
      display: flex;
      justify-content: space-between;
      margin-bottom: 15px;
      .tip {
        height: 34px;
        line-height: 34px;
        font-size: 18px;
        font-weight: 400;
        color: #46545d;
        margin: 0;
        padding: 0;
      }
      .goBack {
        background: #007af5;
        width: 80px;
        height: 34px;
        border: none;
        color: #fff;
        font-size: 12px;
        font-weight: 400;
        cursor: pointer;
      }
    }
    .tabs_item {
      height: calc(100% - 50px);
      background: #fff;
      padding: 20px;
      box-sizing: border-box;
      :deep(.el-tabs) {
        height: 100%;
        .el-tabs__header {
          .el-tabs__nav-wrap::after {
            width: 0;
          }
          .el-tabs__nav-wrap {
            .el-tabs__nav-scroll {
              .el-tabs__nav {
                .el-tabs__active-bar {
                  height: 2px;
                }
                .el-tabs__item {
                  font-size: 16px;
                  font-weight: 400;
                  color: #46545d;
                  &.is-active {
                    font-weight: bold;
                    color: #007af5;
                  }
                }
              }
            }
          }
        }
        .el-tabs__content {
          height: calc(100% - 40px);
          .el-tab-pane {
            height: 100%;
          }
        }
      }
    }
    .contain {
      background: #fff;
      height: calc(100% - 50px);
      .el-container {
        height: 100%;
        .el-header {
          .el-menu--horizontal {
            border-bottom: none;
            .el-menu-item {
              font-size: 16px;
              font-weight: 400;
              color: #46545d;
              &.is-active {
                font-size: 16px;
                font-weight: bold;
                color: #007af5;
                border-bottom: 2px solid #007af5;
              }
            }
          }
        }
        .el-main {
          .main {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            .demo-form-inline {
              display: flex;
              height: 34px;
              max-width: 65%;
              width: 65%;
              //align-items: center;
              :deep(input::placeholder) {
                font-size: 14px;
                font-weight: 400;
                color: #bbbbbb;
              }
              :deep(.el-form-item) {
                margin-bottom: 0;
                line-height: 34px;
                height: 34px;
                width: 40%;
              }
              :deep(.el-form-item__content) {
                line-height: 34px;
                //height: 43px;
                width: 80%;
                :deep(.inline-input) {
                  // width: 230px;
                  height: 34px;
                }
              }
              :deep(.el-input__inner) {
                // width: 200px;
                // height: 34px;
                background: #ffffff;
                // border: 1px solid #dddddd;
                border-radius: 2px;
              }
              :deep(.el-form-item__label) {
                // color: #6fd7f7;
                font-size: 14px;
                font-weight: 400;
                color: #46545d;
                height: 34px;
                line-height: 34px;
              }
            }
            .button {
              display: flex;
              justify-content: space-around;
              max-width: 30%;
              .btn {
                min-width: 80px;
                height: 34px;
                line-height: 34px;
                background: #007af5;
                border-radius: 2px;
                font-size: 12px;
                font-weight: 400;
                color: #ffffff;
                border: 0;
                cursor: pointer;
                &.reset {
                  margin: 0 10px;
                }
                &.xq {
                  min-width: 110px;
                }
                &.rw {
                  margin-left: 10px;
                }
              }
              #goBack {
                max-width: 131px;
                height: 34px;
                background: #007af5;
                border-radius: 2px;
                border: 0;
                font-size: 12px;
                font-weight: 400;
                color: #ffffff;
              }
              .el-dropdown {
                margin: 0px 10px;
              }
              .el-dropdown-link {
                // display: inline-block;
                width: 80px;
                height: 34px;
                line-height: 34px;
                text-align: center;
                color: #fff;
                background: #007af5;
                border-radius: 2px;
              }
            }
          }

          .tableBox {
            height: calc(100% - 50px);
            background: #fff;
            // padding: 20px;
            box-sizing: border-box;
            //overflow: hidden;
            .desc {
              font-size: 12px;
              font-weight: 400;
              color: #ec4b5a;
              background: #fbdbde;
              line-height: 33px;
              height: 33px;
              text-align: center;
              border-radius: 2px;
              display: flex;
              align-items: center;
              justify-content: center;

              img {
                width: 16px;
                height: 16px;
                // margin: auto;
                margin-right: 10px;
              }
              div {
                max-width: 98%;
                .organ {
                  line-height: 33px;
                  max-width: 100%;
                  height: 33px;
                  margin: 0 auto;
                  display: flex;
                  justify-content: space-around;
                  padding-left: 0;
                  li {
                    font-size: 12px;
                    list-style: none;
                    color: #ec4b5a;
                    margin-right: 20px;
                  }
                }
              }
            }
            .table {
              // background: rgba(0, 0, 0, 0.1);
              height: calc(100% - 60px);
              width: 100%;
              margin-bottom: 15px;
              :deep(.el-table-column--selection.is-leaf .el-checkbox) {
                display: none;
              }
              :deep(.el-table__cell.is-leaf) {
                //background: rgba(0, 0, 0, 0.1);
                // color: #6fd7f7;
                //color: #fff;
                color: #929ea8;
              }

              :deep(tr:hover > td) {
                background-color: transparent;
              }
              :deep(.el-table__empty-text) {
                //color: #fff;
                color: #929ea8;
              }
              :deep(td.el-table__cell) {
                //color: #fff;
                color: #929ea8;
              }
            }
            .el-pagination {
              height: 34px;
              padding: 0;
              box-sizing: border-box;
              //width: 50%;
              :deep(.el-pagination__total) {
                font-size: 14px;
                font-weight: 400;
                color: #bbbbbb;
                line-height: 34px;
              }

              :deep(.el-input__inner) {
                height: 34px;
                //width: 60px;
                background: #fff;
                //color: #66b1ff;
                color: #46545d;
              }
              :deep(button:disabled) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(.el-pager li) {
                //background: rgba(0, 0, 0, 0.1);
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
                &.active {
                  // color: #fff;
                  color: #46545d;
                }
              }
              :deep(.btn-prev) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(.btn-next) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(span:not([class*='suffix'])) {
                color: #bbbbbb;
              }
              .el-pagination__jump {
                height: 34px;
                line-height: 34px;
              }
            }
          }
        }
      }
    }
  }
</style>
